package fortunata.fswapps.vpoet;

import com.ecyrd.jspwiki.WikiContext;
import com.ecyrd.jspwiki.plugin.PluginException;
import com.ecyrd.jspwiki.plugin.WikiPlugin;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Map;
import fortunata.FortunataPlugin;

/**
 * This plugin reads a form intended to be used by Visualization Providers (VPs).
 * The form allow VPs to insert visualizations in VPOET
 */
public class WikiPageGenViz extends FortunataPlugin implements WikiPlugin {


    private static Logger log = Logger.getLogger( WikiPageGenViz .class );
    public static String vpPrefix = "vp";

    public String execute (WikiContext context, Map parameterMap) throws PluginException {

        String elem      = FormManager.getFieldNoEmplyValue("elem",     parameterMap, "element");
        String id        = FormManager.getFieldNoEmplyValue("id",       parameterMap, "ID");
        String csscode   = FormManager.getFieldValue       ("csscode",  parameterMap);
        //if (csscode == null) csscode = " ";
        String jscode    = FormManager.getFieldValue("jscode",   parameterMap);
        //if (jscode == null) jscode = " ";
        String htmlcode  = FormManager.getFieldValue("htmlcode", parameterMap);
        //if (htmlcode == null) htmlcode = " ";


        String wikiFileName = getWikiPageName(elem, id);
        String physicalFileName = wikiFileName+ wikiFileExtension;
        FileOutputStream fos = null;
        try {
           fos = new FileOutputStream(getWorkDir(context) + File.separator + physicalFileName);

        }catch (FileNotFoundException e){
           log.error("Could not create file.", e);
           throw new PluginException("Could not create file (see logs)");
        }
        PrintStream ps = new PrintStream(fos);
        ps.println("[{TableOfContents}]"    + wnl +
                   "!!!" + wikiFileName     + wnl +
                   "This can be found at [VPList]"            + nl + nl +
                   "Provides visualization for ["+ elem + "]" + wnl +
                   "Provider ID: "+ id      + wnl +
                   "!!CSS code"             + wnl +
                   "{{{"+ csscode + "}}}"   + wnl +
                   "!!Javascript code"      + wnl +
                   "{{{"+ jscode + "}}}"    + wnl +
                   "!!HTML code"            + wnl +
                   "{{{"+ htmlcode + "}}}"  + wnl);
        //return "You have created <A href='" + context.getEngine().getViewURL(wikiFileName) + "'>"+wikiFileName+"</A>.";
        String wikiText = "%%information "+ nl +
                          "You have created [" + wikiFileName + "]" + nl +
                          "%%";
        //log.info("out=" + Converters.wikiText2HTML(log, context, wikiText));
        return renderWikiText(context, wikiText);
    }



    public static String getWikiPageName(String elem, String id){
        return vpPrefix + "." + elem + "." + id;
    }
}